package org.openrbac.rbac.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.openrbac.rbac.entity.ClassProduct;
import org.openrbac.rbac.entity.Dict;
import org.openrbac.rbac.entity.DictDetail;
import org.openrbac.rbac.mapper.DictDetailMapper;
import org.openrbac.rbac.service.DictDetailService;
import org.openrbac.rbac.service.DictService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
* @author 梁富贵
* @description 针对表【dict_detail(数据字典详情)】的数据库操作Service实现
* @createDate 2024-05-15 15:20:42
*/
@Service
public class DictDetailServiceImpl extends ServiceImpl<DictDetailMapper, DictDetail>
    implements DictDetailService {
    int i = 0;

    @Autowired
    private DictService dictService;

    @Override
    public List<ClassProduct> getClassIdsByDictIds(List<Long> ids) {
        return this.baseMapper.getClassIdsByDictIds(ids);
    }



    @Override
    public List<Dict> getDictDetailsByMainNames(List<String> names) {
        if (names == null || names.isEmpty()) {
            return Collections.emptyList();
        }

        List<Dict> firsts = dictService.getDictsByNames(names);
        // 使用Stream API来简化逻辑和提高性能
        return firsts.stream()
                // 将每个Dict的子元素填充完成
                .map(this::populateDictChildren)
                .collect(Collectors.toList());
    }

    private Dict populateDictChildren(Dict dict) {
        // 防御性编程
        if (dict == null) {
            log.warn("dict is null");
            return dict;
        }

        List<DictDetail> seconds = this.baseMapper.getDictDetailsByDictIds(Collections.singletonList(dict.getId()));

        if(Objects.isNull(seconds)){
            return dict;
        }

        dict.setChildren(populateDictDetailChildren(seconds));
        return dict;
    }

    private List<DictDetail> populateDictDetailChildren(List<DictDetail> details) {
        if (details == null || details.isEmpty()) {
            return Collections.emptyList();
        }

        // Collect all detail IDs for batch retrieval
        List<Long> detailIds = details.stream()
                .map(DictDetail::getId)
                .collect(Collectors.toList());

        //用于下方过滤
        List<ClassProduct> classProducts = getClassIdsByDictIds(detailIds);

        List<Long> ids = classProducts.stream()
                .map(classProduct -> (long)classProduct.getNid())
                .collect(Collectors.toList());

        List<Long> pids = classProducts.stream()
                .map(classProduct -> (long)classProduct.getPid())
                .collect(Collectors.toList());

        List<DictDetail> third;
        if(!ids.isEmpty()){
            third = this.baseMapper.getDictDetailsByIds(ids);
        }else {
            return details;
        }

        // Map children for each DictDetail
        return details.stream()
                .peek(detail -> {
                    if (detail.getChildren() == null) {
                        detail.setChildren(new ArrayList<>());
                    }
                    if (third == null || third.isEmpty()) {
                        return;
                    }
                    List<DictDetail> children = third.stream()
                            .filter(thirdDetail -> ids.contains(thirdDetail.getId()))
                            .filter(thirdDetail -> pids.contains(detail.getId()))
                            .collect(Collectors.toList());
                    populateDictDetailChildren(children);
                    detail.getChildren().addAll(children);
                })
                .collect(Collectors.toList());
    }
}





